package com.seanliao.nav.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seanliao.nav.common.domain.vo.ArticleVO;
import com.seanliao.nav.entity.Article;
import com.seanliao.nav.mapper.ArticleMapper;
import com.seanliao.nav.service.IArticleService;
import com.seanliao.nav.util.CommonUtil;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author Sean_Liao
 * @since 2024-03-06
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {

    @Override
    public IPage<ArticleVO> listArticle(IPage<Map<String, Object>> pp, QueryWrapper<?> qw) {
        IPage<Map<String, Object>> mapIPage = this.getBaseMapper().listArticle(pp, qw);
        return mapIPage.convert(
                map -> {
                    ArticleVO articleVO = BeanUtil.mapToBean(map, ArticleVO.class, true, null);
                    articleVO.setTagList(CommonUtil.parseTags((String) map.get("tags")));
                    articleVO.setPublishTime(DateUtil.format((LocalDateTime) map.get("publish_time"), DatePattern.NORM_DATETIME_PATTERN));
                    return articleVO;
                });
    }
}
